package cn.iocoder.yudao.module.ao.service.patrol;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.patrol.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.patrol.PatrolDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.patrol.PatrolMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 巡班违纪记录 Service 实现类
 *
 * @author 智教云枢
 */
@Service
@Validated
public class PatrolServiceImpl implements PatrolService {

    @Resource
    private PatrolMapper patrolMapper;

    @Override
    public Long createPatrol(PatrolSaveReqVO createReqVO) {
        // 插入
        PatrolDO patrol = BeanUtils.toBean(createReqVO, PatrolDO.class);
        patrolMapper.insert(patrol);

        // 返回
        return patrol.getId();
    }

    @Override
    public void updatePatrol(PatrolSaveReqVO updateReqVO) {
        // 校验存在
        validatePatrolExists(updateReqVO.getId());
        // 更新
        PatrolDO updateObj = BeanUtils.toBean(updateReqVO, PatrolDO.class);
        patrolMapper.updateById(updateObj);
    }

    @Override
    public void deletePatrol(Long id) {
        // 校验存在
        validatePatrolExists(id);
        // 删除
        patrolMapper.deleteById(id);
    }

    @Override
        public void deletePatrolListByIds(List<Long> ids) {
        // 删除
        patrolMapper.deleteByIds(ids);
        }


    private void validatePatrolExists(Long id) {
        if (patrolMapper.selectById(id) == null) {
            throw exception(PATROL_NOT_EXISTS);
        }
    }

    @Override
    public PatrolDO getPatrol(Long id) {
        return patrolMapper.selectById(id);
    }

    @Override
    public PageResult<PatrolDO> getPatrolPage(PatrolPageReqVO pageReqVO) {
        return patrolMapper.selectPage(pageReqVO);
    }

}